package tools;

import java.util.ArrayList;

import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

public class JsonTest {
	public static void main(String[] args) {
		// String categoryString
		// =FileUtil.readFileByLines("/home/dsw/workspaces/workspace07/operator/src/main/webapp/data/category");
		String categoryString = "{'id':0,'value':'Root',"
				+ "'children':[{'id':10108,'value':'人口属性','children':"
				+ "[{'id':10109,'value':'性别','children':[{'id':10110,'value':'男性','children':null},"
				+ "{'id':10111,'value':'女性','children':null}]},{'id':10112,'value':'年龄',"
				+ "'children':[{'id':10113,'value':'7-12','children':null},{'id':10114,'value':'13-18','children':null},"
				+ "{'id':10115,'value':'19-22','children':null},{'id':10116,'value':'23-28','children':null},"
				+ "{'id':13861,'value':'29-35','children':null},{'id':10117,'value':'36-50','children':null},"
				+ "{'id':10118,'value':'50岁以上','children':null}]},{'id':10119,'value':'月收入','children':"
				+ "[{'id':10120,'value':'4000元以下','children':null},{'id':10123,'value':'4000-8000元','children':null},"
				+ "{'id':10125,'value':'8001-12000元','children':null},{'id':10126,'value':'12001-20000元','children':null},"
				+ "{'id':10127,'value':'20000以上','children':null}]},{'id':10128,'value':'身份职业','children':[{'id':10129,"
				+ "'value':'学生','children':null},{'id':10130,'value':'商务白领','children':null}]}]}]}";
		
		JSONObject category = JSONObject.fromObject(categoryString);
		//CategoryEntity entity = (CategoryEntity) JSONObject.toBean(category,CategoryEntity.class);
		//System.out.println(entity.getId());
		//System.out.println(entity.getValue());
		String idString = "0";
		Object jo = JsonTraversalToGetChildren(category, idString);
		System.out.println(jo);
		/*JSONObject resultJSON = JsonTraversalToGetStructure(category, idString);
		System.out.println(resultJSON);
		jo = null;
		JSONArray array = JSONArray.fromObject(jo);
		System.out.println(array);*/

		// 1.读某个节点的所有children
		// 2.
		// 3.标示是否是族群，也就是说是否全是或
		// 4.单个的树结构
		// {id,value,children[{id,value,children[{id,value,children:null}]}]}
	}
	public static JSONObject JsonTraversalToGetStructure(JSONObject category,String idString){
		ArrayList<String> result = JsonTraversalToGetStructureArray(category, idString, new ArrayList<String>());
		if(result!=null){
			return getResultFromList(category, result);
		}else {
			return new JSONObject();
		}
	}	

	private static JSONObject getResultFromList(JSONObject category, ArrayList<String> list) {
		JSONObject currentCategory = category;
		ArrayList<JSONObject> resultList = new ArrayList<JSONObject>();
		for(int i=0;i<list.size();i++){
			JSONObject childJSON = new JSONObject();
			childJSON.put("id", currentCategory.get("id"));
			childJSON.put("value", currentCategory.get("value"));
			resultList.add(childJSON);
			if(i==list.size()-1){
				break;
			}
			JSONArray jsonArray = (JSONArray) currentCategory.get("children");
			currentCategory = null;
			for (int j = 0; j < jsonArray.size(); j++) {
				if (list.get(i + 1).equalsIgnoreCase(jsonArray.getJSONObject(j).get("id").toString())) {
					currentCategory = jsonArray.getJSONObject(j);
					break;
				}
			}
		}
		for(int i=resultList.size()-2;i>=0;i--){
			JSONArray jsonArray = new JSONArray();
			jsonArray.add(resultList.get(i+1));
			resultList.get(i).put("children", jsonArray);
		}
		return resultList.get(0);
	}

	private static ArrayList<String> JsonTraversalToGetStructureArray(JSONObject category, String idString,
			ArrayList<String> list) {
		if (idString.equalsIgnoreCase(category.get("id").toString())) {
			list.add(idString);
			return list;
		}
		ArrayList<String> currentList = new ArrayList<String>();
		currentList.addAll(list);
		currentList.add(category.get("id").toString());
		Object children = category.get("children");
		if (children != null && !(children instanceof JSONNull)) {
			JSONArray jsonArray = (JSONArray) children;
			for (int i = 0; i < jsonArray.size(); i++) {
				ArrayList<String> result = JsonTraversalToGetStructureArray(jsonArray.getJSONObject(i), idString,
						currentList);
				if (result != null) {
					return result;
				}
			}
		}
		return null;
	}

	private static Object JsonTraversalToGetChildren(JSONObject category, String idString) {
		if (idString.equalsIgnoreCase(category.get("id").toString())) {
			return category.get("children");
		}
		Object children = category.get("children");
		if (children != null && !(children instanceof JSONNull)) {
			JSONArray jsonArray = (JSONArray) children;
			for (int i = 0; i < jsonArray.size(); i++) {
				Object result = JsonTraversalToGetChildren(jsonArray.getJSONObject(i), idString);
				if (result != null) {
					return result;
				}
			}
		}
		return null;
	}
}
